package com.juzhencms.apps.busi.web;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.busi.service.OrganizationService;
import com.juzhencms.apps.busi.service.RoleService;
import com.juzhencms.apps.busi.service.SiteService;
import com.juzhencms.apps.busi.service.TemplateService;

import lombok.extern.slf4j.Slf4j;

import com.juzhencms.apps.base.busi.service.WeixinApiService;
import com.juzhencms.apps.base.common.config.Config;
import com.juzhencms.apps.base.common.database.DBTable;
import com.juzhencms.apps.base.common.util.StringUtil;
import com.juzhencms.apps.base.common.util.WebUtil;

@Controller
@Slf4j
@RequestMapping("/site")
public class SiteController  extends BaseController{
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private SiteService siteService;
	
	@Autowired
	private TemplateService templateService;
	
	@Autowired
	private OrganizationService organizationService;
	
	@Autowired
	private WeixinApiService weixinApiService;
	
	@RequestMapping(value="/gridlistData",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String gridlistData(HttpServletRequest request){
		Map data=new HashMap();
		List whereParamList=new ArrayList();
//		String where="where del_flag=0";
		String where=" del_flag=0 ";
		
		//机构筛选条件
		if( StringUtil.isNotNullOrEmpty(request.getParameter("org_id")) && !request.getParameter("org_id").equals("0")){
			where+=" and find_in_set(org_id,?) ";
			int org_id = new Integer(request.getParameter("org_id").toString());
			String getOrgIds = organizationService.getOrgIds(org_id);
			whereParamList.add(getOrgIds);
		}
		
		/*//根据语言查询
		if(request.getParameter("language")!=null && !request.getParameter("language").toString().trim().equals("")){
			where+=" and language = ? ";
			whereParamList.add(request.getParameter("language"));
		}
		//根据地区查询
		if(request.getParameter("area_id")!=null && !request.getParameter("area_id").toString().trim().equals("")){
			where+=" and area_id = ? ";
			whereParamList.add(request.getParameter("area_id"));
		}
		//根据模板查询
		if(request.getParameter("template")!=null && !request.getParameter("template").toString().trim().equals("")){
			where+=" and template = ? ";
			whereParamList.add(request.getParameter("template"));
		}*/
		//server_config
		
		if(request.getParameter("server_config")!=null && !request.getParameter("server_config").toString().trim().equals("")){
			where+=" and server_config = ? ";
			whereParamList.add(request.getParameter("server_config"));
		}
		if(request.getParameter("open_app_menu")!=null && !request.getParameter("open_app_menu").toString().trim().equals("")){
			where+=" and open_app_menu = ? ";
			whereParamList.add(request.getParameter("open_app_menu"));
		}
		if(request.getParameter("open_app_reply")!=null && !request.getParameter("open_app_reply").toString().trim().equals("")){
			where+=" and open_app_reply = ? ";
			whereParamList.add(request.getParameter("open_app_reply"));
		}
		if(request.getParameter("open_app_message")!=null && !request.getParameter("open_app_message").toString().trim().equals("")){
			where+=" and open_app_message = ? ";
			whereParamList.add(request.getParameter("open_app_message"));
		}
		
		//搜索参数
		String searchVar=null;
		if(request.getParameter("varValue")!=null && !request.getParameter("varValue").toString().trim().equals("")){
			whereParamList.add("%"+request.getParameter("varValue")+"%");
			if(request.getParameter("searchVar")!=null && !request.getParameter("searchVar").toString().trim().equals("")){
				searchVar=request.getParameter("searchVar");
				where+=" and "+searchVar+" like ? ";
			}else{
				where+=" and name like ? ";
			}
		}
		
		
		
		if(request.getParameter("id")!=null && !request.getParameter("id").toString().trim().equals("")){
			where+=" and id=? ";
			whereParamList.add(request.getParameter("id"));
		}		
		if(request.getParameter("name")!=null && !request.getParameter("name").toString().trim().equals("")){
			where+=" and name like ? ";
			whereParamList.add("%"+request.getParameter("name")+"%");
		}
		if(request.getParameter("weixin_no")!=null && !request.getParameter("weixin_no").toString().trim().equals("")){
			where+=" and weixin_no like ? ";
			whereParamList.add("%"+request.getParameter("weixin_no")+"%");
		}		
		if(request.getParameter("publish")!=null && !request.getParameter("publish").toString().trim().equals("")){
			where+=" and publish = ? ";
			whereParamList.add(request.getParameter("publish"));
		}
		if(request.getParameter("site_id")!=null && !request.getParameter("site_id").toString().trim().equals("")){
			where+=" and id = ? ";
			whereParamList.add(request.getParameter("site_id"));
		}
		
		if(request.getParameter("create_time_from")!=null && !request.getParameter("create_time_from").toString().trim().equals("")){
			where+=" and create_time >= ? ";
			whereParamList.add(request.getParameter("create_time_from"));
		}
		
		if(request.getParameter("create_time_to")!=null && !request.getParameter("create_time_to").toString().trim().equals("")){
			where+=" and create_time <= ? ";
			whereParamList.add(request.getParameter("create_time_to"));
		}
		
		//分页处理
		int pageIndex=1;
		if(request.getParameter("pageIndex")!=null && !request.getParameter("pageIndex").toString().trim().equals("")){
			pageIndex=new Integer(request.getParameter("pageIndex")).intValue();
		}
		int pageSize=10;
		if(request.getParameter("pageSize")!=null && !request.getParameter("pageSize").toString().trim().equals("")){
			pageSize=new Integer(request.getParameter("pageSize")).intValue();
		}
//		int start=0;
//		if(pageIndex>1){
//			start=(pageIndex-1)*pageSize;
//		}
//		whereParamList.add(start);
//		whereParamList.add(pageSize);
		
		//排序参数 
		String sortField="position";
		String sortOrder="asc";
		if(request.getParameter("sortField")!=null && !request.getParameter("sortField").equals(""))
			sortField = request.getParameter("sortField");
		if(request.getParameter("sortOrder")!=null && !request.getParameter("sortOrder").equals(""))
			sortOrder = request.getParameter("sortOrder");
		
		//查询列表数据
//		List dataList=this.getCommonService().getJdbcDao().querySql("select * from "+DBTable.site+" "+where+" order by "+sortField+" "+sortOrder+",id asc limit ?,?", whereParamList.toArray());
		String orderby = " order by "+sortField+" "+sortOrder+",id asc ";
		List dataList = siteService.getLists("id,type,name,weixin_no,org_name,publish,position,case when (domain_protocol='http://' and domain_port='80') or (domain_protocol='https://' and domain_port='443') or domain_port is null or domain_port='' then concat(domain_protocol,domain,domain_path) else concat(domain_protocol,domain,':',domain_port,domain_path) end as domain,template_name,template_type,template_code,template_place,create_time", where, whereParamList, orderby, pageIndex, pageSize,0);
		
		if(dataList!=null && dataList.size()>0){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			for(int i=0;i<dataList.size();i++){
				Map row=(Map)dataList.get(i);
				Date date = (Date)row.get("create_time");
				row.put("create_time",sdf.format(date));
				
			}
		}
		
		//查询总条数
		
//		Map countRow=this.getCommonService().getJdbcDao().queryRow("select count(id) as count_num from "+DBTable.site+" "+where, whereParamList.subList(0, whereParamList.size()-2).toArray());
		Map countRow = siteService.getDetail(" count(id) as count_num ", where,  whereParamList.subList(0, whereParamList.size()-2), "",0);
		
		int recordsTotal=0;
		if(countRow.get("count_num")!=null){
			recordsTotal=new Integer(countRow.get("count_num").toString()).intValue();
		}
		
		//拼装返回的数据
		data.put("data", dataList.toArray());
		data.put("itemsCount", recordsTotal);
		
		return WebUtil.resultString(data,request);
	}
	
	
	
	//暂不采用
	@RequestMapping(value="/sitelistData",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String sitelistData(HttpServletRequest request){
		Map data=new HashMap();
		List whereParamList=new ArrayList();
		String where="where del_flag=0";
		
		
		//排序参数 
		String sortField="position";
		String sortOrder="asc";
		if(request.getParameter("sortField")!=null && !request.getParameter("sortField").equals(""))
			sortField = request.getParameter("sortField");
		if(request.getParameter("sortOrder")!=null && !request.getParameter("sortOrder").equals(""))
			sortOrder = request.getParameter("sortOrder");
		
		//查询列表数据
		List dataList=this.getCommonService().getJdbcDao().querySql("select id,name from "+DBTable.site+" "+where+" order by "+sortField+" "+sortOrder+",id asc");
		if(dataList!=null && dataList.size()>0){
			//拼装返回的数据
			data.put("data", dataList.toArray());
		}
		
		return WebUtil.resultString(data,request);
	}

	
	@RequestMapping(value="/detailData/{id}",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String detailData(@PathVariable("id") String id,HttpServletRequest request){
		Map data=new HashMap();
//		Map record=this.getCommonService().getJdbcDao().queryRow("select id,name,domain,domain_alias,site_alias,language,template,area_id,logo,ext_header,seo_title,seo_keywords,seo_description,template,copyright,create_time,org_id,org_name from "+DBTable.site+" where id=?",new Object[]{id});	

		
		String where = " del_flag=0 and id = ? ";	
		List whereParamList = new ArrayList();
		whereParamList.add(new Integer(id));
		Map record = siteService.getDetail("name,org_name,id,create_time,weixin_no,type,logo,org_id,weixin_appid,weixin_appsecret,weixin_EncodingAESKey,weixin_Token,publish,server_config,weixin_access_token_api,open_app_menu,open_app_reply,open_app_message ", where, whereParamList, "",0);		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = (Date)record.get("create_time");
		record.put("create_time",sdf.format(date) );
		//
		data.put("data", record);
		data.put("url", Config.getValue("app.domain")+"replyMessage/receive/"+record.get("id"));
		
		return WebUtil.resultString(data,request);
	}
	
	/*@RequestMapping(value="/save",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String save(HttpServletRequest request) throws UnsupportedEncodingException{
		
		
		//System.out.println(request.getParameter("data"));
		Map data = JSON.parseObject(request.getParameter("data"), Map.class); 
		if(StringUtil.isNotNullOrEmpty(data.get("name"))){
			data.put("name",java.net.URLDecoder.decode((data.get("name").toString()), "UTF-8"));
		}
		data.put("del_flag", 0);
		String id=null;
		if(data.get("id")!=null){
			id=data.get("id").toString();
		}
		
		//验证输入的域名和目录是否有冲突
		//获取当前域名，拼接正则表达式
		if(data.get("domain")==null || data.get("domain").toString().trim().equals("")) {
			Map result=new HashMap();
			result.put("result", 0);
			result.put("msg", "请输入正确的访问域名");
			return WebUtil.resultString(result,request);
		}else {
			String domain=data.get("domain").toString();
			
			List domainList=new ArrayList();
			domainList.add(domain.replace(".", "\\."));
			if(data.get("domain_alias")!=null) {
				String[] domainAliasArray=data.get("domain_alias").toString().split(",");
				for(int i=0;i<domainAliasArray.length;i++) {
					String tmpDomain=domainAliasArray[i];
					if(tmpDomain!=null && !tmpDomain.trim().equals("")) {
						domainList.add(tmpDomain.replace(".", "\\."));
					}
				}
			}
			
			String domainReg="^(.+,)?("+org.apache.commons.lang3.StringUtils.join(domainList,"|")+")(,.+)?$";
			List whereParamList=new ArrayList();
			String siteWhere=" where del_flag=0 and (domain REGEXP ? or domain_alias REGEXP ?) ";
			whereParamList.add(domainReg);
			whereParamList.add(domainReg);
			
			if(id!=null && !id.equals("")) {
				siteWhere+=" and id!=? ";
				whereParamList.add(id);
			}
			
			//根据有无目录，执行不同的查询
			if(data.get("domain_path")==null || data.get("domain_path").toString().equals("") || data.get("domain_path").toString().equals("/")) {
				siteWhere+=" and (domain_path is null or domain_path='' or domain_path='/') ";
			}else {
				siteWhere+=" and domain_path=? ";
				whereParamList.add(data.get("domain_path"));
			}
			
			//判断端口(这里的逻辑比较复杂)
			if(data.get("domain_port")==null || data.get("domain_port").toString().equals("")) {
				if(data.get("domain_protocol").toString().equals("http://")) {
					siteWhere+=" and (domain_port='80' or (domain_protocol='http://' and (domain_port='' or domain_port is null))) ";
				}else if(data.get("domain_protocol").toString().equals("https://")){
					siteWhere+=" and (domain_port='443' or (domain_protocol='https://' and (domain_port='' or domain_port is null))) ";
				}
			}else {
				String port=data.get("domain_port").toString();
				if(data.get("domain_port").toString().equals("80")) {
					siteWhere+=" and (domain_port='80' or (domain_protocol='http://' and (domain_port='' or domain_port is null))) ";
				}else if(data.get("domain_port").toString().equals("443")){
					siteWhere+=" and (domain_port='443' or (domain_protocol='https://' and (domain_port='' or domain_port is null))) ";
				}else {
					siteWhere+=" and domain_port=?  ";
					whereParamList.add(data.get("domain_port"));
				}
			}
			
			Map tmpSite=this.getCommonService().getJdbcDao().queryRow("select id from "+DBTable.site+siteWhere+" limit 0,1",whereParamList.toArray());
			if(tmpSite!=null) {
				Map result=new HashMap();
				result.put("result", 0);
				result.put("msg", "已存在域名、端口及目录均相同的站点，请检查修改");
				return WebUtil.resultString(result,request);
			}
			
		}
		
		
		
		
		//机构org_id
		if(StringUtil.isNullOrEmpty(data.get("org_id"))) {
			Map result=new HashMap();
			result.put("result", 0);
			result.put("msg", "保存失败，请选择机构");
			return WebUtil.resultString(result,request);
		} else {
			int org_id = new Integer(data.get("org_id").toString());
			if(org_id <=0) {
				Map result=new HashMap();
				result.put("result", 0);
				result.put("msg", "保存失败，请选择机构");
				return WebUtil.resultString(result,request);
			}
			data.put("org_id",org_id );
			//机构名称  
			Map getOrg = this.getCommonService().getJdbcDao().queryRow("select name from "+DBTable.organization+" where id=? ",new Object[]{org_id});
			String org_name = "";
			if(getOrg!=null && !getOrg.get("name").equals("") ) {
				org_name = getOrg.get("name").toString();
			}
			data.put("org_name",org_name);	
		}	
		
		Date date = new Date();
		if(id==null || id.equals("")){
			if(data.get("create_time")==null || data.get("create_time").equals("")){
				data.put("create_time", date);
			}
			data.put("update_time", date);
			int siteId=this.getCommonService().getJdbcDao().addObj(DBTable.site, data);
			
			//如果存在外部模板，则需要根据外部模板配置初始化栏目及广告，以及添加测试数据
			if(data.get("template_place")!=null && data.get("template_place").toString().equals("1") && data.get("template_code")!=null) {
				boolean addTestData=false;
				if(data.get("test_data")!=null && data.get("test_data").toString().equals("1")) {
					addTestData=true;
				}
				
				//初始化站点栏目、广告、测试数据等
				boolean res=templateService.initTemplateSite(siteId, data.get("template_code").toString(), addTestData);
				if(res) {
					Date now=new Date();
					if(data.get("template_type")!=null && !data.get("template_type").toString().equals("")) {
						int templateType=Integer.parseInt(data.get("template_type").toString());
						if(templateType==2) {//如果选择了复制模板，增加模板记录
							Map newTemplate=this.getCommonService().getJdbcDao().queryRow("select * from "+DBTable.template+" where code=? and site_id=0 and del_flag=0 order by id asc limit 0,1", new Object[] {data.get("template_code")});
							newTemplate.remove("id");
							newTemplate.put("template_type", 2);
							newTemplate.put("site_id", siteId);
							newTemplate.put("create_time", now);
							newTemplate.put("update_time", now);
							this.getCommonService().getJdbcDao().addObj(DBTable.template, newTemplate);
						}else if(templateType==3) {//如果选择了独立模板，将站点ID更新到模板表
							this.getCommonService().getJdbcDao().updateSql("update "+DBTable.template+" set site_id=? where code=? and del_flag=0 and site_id=0", new Object[] {data.get("template_code")});
						}
					}
				}
			}
		}else{
			data.put("update_time", date);
			this.getCommonService().getJdbcDao().updateObj(DBTable.site, data, "id");
		}
		
		Map result=new HashMap();
		result.put("result", 1);
		result.put("msg", "保存成功");
		
		return WebUtil.resultString(result,request);
		
	}*/
	
	
	@RequestMapping(value="/save",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String save(HttpServletRequest request) throws UnsupportedEncodingException{
		
		Map result=new HashMap();
		result.put("needConfig",0);	
		//System.out.println(request.getParameter("data"));
		Map data = JSON.parseObject(request.getParameter("data"), Map.class); 
		if(StringUtil.isNotNullOrEmpty(data.get("name"))){
			data.put("name",java.net.URLDecoder.decode((data.get("name").toString()), "UTF-8"));
		}
		data.put("del_flag", 0);
		String id=null;
		
		//判断微信公众号，不重复
		
		if(StringUtil.isNullOrEmpty(data.get("weixin_appid"))) {
			result.put("result", 0);
			result.put("msg", "请输入正确的weixin_appid");
			return WebUtil.resultString(result,request);
		}
		if(StringUtil.isNullOrEmpty(data.get("weixin_no"))) {
			result.put("result", 0);
			result.put("msg", "请输入正确的weixin_no,微信号");
			return WebUtil.resultString(result,request);
		}
		if(StringUtil.isNullOrEmpty(data.get("weixin_appsecret")) && StringUtil.isNullOrEmpty(data.get("weixin_access_token_api"))) {
			result.put("result", 0);
			result.put("msg", "appsecret与accessToken接口不能同时为空");
			return WebUtil.resultString(result,request);
		}
		
		//如果选择开启自定义回复，需要启用服务器配置
		if(!StringUtil.isNullOrEmpty(data.get("open_app_reply")) && data.get("open_app_reply").toString().equals("1")) {
			if(StringUtil.isNullOrEmpty(data.get("server_config")) || !data.get("server_config").toString().equals("1")) {
				result.put("result", 0);
				result.put("msg", "开启自定义回复功能必须先开启服务器配置");
				return WebUtil.resultString(result,request);
			}
		}
		
		String sql="SELECT * FROM "+DBTable.site+" where del_flag=0 and (weixin_appid=? OR weixin_no=?) ";
		List paramWhereList=new ArrayList();
		paramWhereList.add(data.get("weixin_appid"));
		paramWhereList.add(data.get("weixin_no"));
		if(data.get("id")!=null){
			id=data.get("id").toString();
			sql+="and  id!=? ";
			paramWhereList.add(id);
		}
		sql+=" limit 0,1 ";
		Map _temp=this.getCommonService().getJdbcDao().queryRow(sql,paramWhereList.toArray());
		if(_temp!=null) {
			result.put("result", 0);
			result.put("msg", "抱歉当前微信号或是appid重复请核对！");
			return WebUtil.resultString(result,request);
		}
		
		
		//机构org_id
		if(StringUtil.isNullOrEmpty(data.get("org_id"))) {
			result.put("result", 0);
			result.put("msg", "保存失败，请选择机构");
			return WebUtil.resultString(result,request);
		} else {
			int org_id = new Integer(data.get("org_id").toString());
			if(org_id <=0) {
				result.put("result", 0);
				result.put("msg", "保存失败，请选择机构");
				return WebUtil.resultString(result,request);
			}
			data.put("org_id",org_id );
			//机构名称  
			Map getOrg = this.getCommonService().getJdbcDao().queryRow("select name from "+DBTable.organization+" where id=? ",new Object[]{org_id});
			String org_name = "";
			if(getOrg!=null && !getOrg.get("name").equals("") ) {
				org_name = getOrg.get("name").toString();
			}
			data.put("org_name",org_name);	
		}	
		
		
		
		
		Date date = new Date();
		if(id==null || id.equals("")){ //第一次保存校验微信公众号是否正确
			
			Map accessToken=null;
			
			if(data.get("weixin_access_token_api")!=null && !data.get("weixin_access_token_api").toString().equals("")) {
				accessToken=this.weixinApiService.checkAccessTokenApi(data.get("weixin_appid")+"", data.get("weixin_appsecret")+"",data.get("weixin_access_token_api").toString());
				
			}else {
				accessToken=this.weixinApiService.getAccessTokenCheckInfo(data.get("weixin_appid")+"", data.get("weixin_appsecret")+"");
				
			}
			
			
			
			
			log.info("accessToken=="+accessToken);
			
			if((accessToken.get("result")+"").equals("1")) {
				if(data.get("create_time")==null || data.get("create_time").toString().equals("")){
					data.put("create_time", date);
				}
				data.put("update_time", date);
				data.put("create_user", this.getLoginUserId());
				int siteId=this.getCommonService().getJdbcDao().addObj(DBTable.site, data);
				log.info("siteId=="+siteId);
				result.put("id", siteId);
				result.put("needConfig",1);
			}else {
				result.put("result", 0);
				result.put("msg", "您填写的公众号appid和appsecret校验失败，错误信息："+accessToken.get("msg"));
				return WebUtil.resultString(result,request);
			}			
		}else{
			data.put("update_time", date);
			result.put("id", data.get("id"));
			this.getCommonService().getJdbcDao().updateObj(DBTable.site, data, "id");
		}		
		result.put("result", 1);
		result.put("msg", "保存成功");		
		return WebUtil.resultString(result,request);
		
	}
	
	@RequestMapping(value="/delete",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String delete(HttpServletRequest request){

		
		Map data = JSON.parseObject(request.getParameter("data"), Map.class); 
		
		String id=null;
		if(data.get("id")!=null){
			id=data.get("id").toString();
		}
		
		
		if(id==null || id.equals("")){
		}else{	
	        String[] idArray = id.split(",");
	        String siteIdVarStr="?";
	        if(idArray!=null && idArray.length>0){
	        	Matcher matcher = Pattern.compile("\\d+").matcher(id);
	        	siteIdVarStr=matcher.replaceAll("?");

	        	ArrayList siteIdParamList=new ArrayList();
		        
		        for(int i=0;i<idArray.length;i++) {
		        	siteIdParamList.add(idArray[i]);
		        }
		        
		        //查询站点关联的栏目
		        List menuList=this.getCommonService().getJdbcDao().querySql("select id from "+DBTable.menu+" where site_id in ("+siteIdVarStr+")",siteIdParamList.toArray());

				if(menuList!=null && menuList.size()>0) {
					List menuIdList=new ArrayList();
					for(int i=0;i<menuList.size();i++) {
						Map tmpMenu=(Map)menuList.get(i);
						menuIdList.add(tmpMenu.get("id"));
					}
					
					String menuIdStr=org.apache.commons.lang3.StringUtils.join(menuIdList, ",");
					
					//删除站点下的新闻
			        this.getCommonService().getJdbcDao().updateSql("update "+DBTable.news+" set del_flag=1 where del_flag=0 and menu_id in ("+menuIdStr+")");

			        
			        //删除站点下的文章
			        this.getCommonService().getJdbcDao().updateSql("update "+DBTable.article+" set del_flag=1 where del_flag=0 and menu_id in ("+menuIdStr+")");

			        
			        //删除站点下的友情链接
			        this.getCommonService().getJdbcDao().updateSql("update "+DBTable.links+" set del_flag=1 where del_flag=0 and menu_id in ("+menuIdStr+")");
			        
			        //删除站点下的栏目
			        this.getCommonService().getJdbcDao().updateSql("update "+DBTable.menu+" set del_flag=1 where del_flag=0 and id in ("+menuIdStr+")");
					
				}
				//删除站点下的广告
				this.getCommonService().getJdbcDao().updateSql("update "+DBTable.ad+" set del_flag=1 where del_flag=0 and site_id in ("+siteIdVarStr+")",siteIdParamList.toArray());
		        
		        //删除站点下的广告位
				this.getCommonService().getJdbcDao().updateSql("update "+DBTable.adSpace+" set del_flag=1 where del_flag=0 and site_id in ("+siteIdVarStr+")",siteIdParamList.toArray());

				//删除站点
				this.getCommonService().getJdbcDao().updateSql("update "+DBTable.site+" set del_flag=1 where del_flag=0 and id in ("+siteIdVarStr+")",siteIdParamList.toArray());

	        }
		}

		Map result=new HashMap();
		result.put("result",1);
		result.put("msg", "操作成功");
		
		return WebUtil.resultString(result,request);
	}
	
	//更新发布状态
	@RequestMapping(value="/updatePublish",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String updatePublish(HttpServletRequest request){
		String num = "1".equals(request.getParameter("publish"))?"0":"1";
		this.getCommonService().getJdbcDao().updateSql("update "+DBTable.site+" set publish=? where id=?",new Object[]{num,request.getParameter("id")});
		Map result=new HashMap();
		result.put("result",1);
		result.put("msg", "操作成功");
		
		return WebUtil.resultString(result,request);
	}
	
	@RequestMapping(value="/updatePosition",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String updatePosition(HttpServletRequest request){
		this.getCommonService().getJdbcDao().updateSql("update "+DBTable.site+" set position=? where id=?",new Object[]{request.getParameter("position"),request.getParameter("id")});
		Map result=new HashMap();
		result.put("result",1);
		result.put("msg", "操作成功");
		
		return WebUtil.resultString(result,request);
	}
	

	/**
	 * 根据当前用户权限返回可以操作的站点
	 * @param request
	 * @return
	 */
	/*@RequestMapping(value="/siteTreeData",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String siteTreeData(HttpServletRequest request){
		Map result=new HashMap();
		
		String where = " del_flag=0  ";
		//用户站点权限
		String getPermissionSiteIds = this.getPermissionSiteIds();
		if(StringUtil.isNotNullOrEmpty(getPermissionSiteIds) ) {
			where += " and id in ("+getPermissionSiteIds+") ";
		}
		//条件：未删除、未设置机构条件、站点在管理员用户可管理范围内
		List dataList = siteService.getLists("  id ,pid as 'pId',name  ", where, null, "", 0, 0,1);
		if(dataList!=null) {
			for(int i=0;i<dataList.size();i++) {
				Map row=(Map)dataList.get(i);
				if(row.get("id").toString().equals(request.getParameter("site_id"))) {
					row.put("checked", true);
					result.put("value",row.get("id"));
					result.put("text",row.get("name"));
				}
					
			}
		}
		result.put("result",1);
		result.put("list", dataList);
		
		return WebUtil.resultString(result,request);
	}*/
}
